// pages/quanbu_dingdan/quanbu_dingdan.js
const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgurl: app.data.imgurl,
        navIndex: 0,
        page: 0,
        lists: [],
        statusarr: [], //状态名称
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.setStorageSync('prevurl','order');
        if (options.state) {
            this.setData({
                navIndex: options.state
            })
        }
        //获取订单列表
        this.getorders();
    },
    //获取订单列表
    getorders: function() {
        var that = this;
        var page = that.data.page;
        page++;
        that.setData({
            page: page
        })
        var userid = app.globalData.userId;
        //仅显示
        util.httpRrquest(app.data.rooturl + 'order/getMyOrder', 'GET', {
                type: that.data.navIndex,
                page: page,
                userid:userid
            })
            .then(function(response) {
                if (that.data.page == 1) {
                    if (response.data.data.orderlist.length > 0) {
                        that.setData({
                            lists: response.data.data.orderlist,
                            statusarr: response.data.data.statusarr
                        })
                    } else {
                        wx.showToast({
                            title: '暂无数据...',
                            icon: 'loading'
                        })
                    }
                } else {
                    if (response.data.data.orderlist.length > 0) {
                        var lists = that.data.lists;
                        that.setData({
                            lists: lists.concat(response.data.data.orderlist)
                        })
                    } else {
                        wx.showToast({
                            title: '暂无更多数据...',
                            icon: 'loading'
                        })
                    }
                }
            }, function(error) {
                console.log(error);
            });
    },
    //点击tab切换
    navTab(e) {
        console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        this.setData({
            navIndex: id,
            page: 0,
            lists: []
        })
        //获取订单列表
        this.getorders();
    },
    
    cancelorder: function (e) {//取消订单
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确定要取消此订单？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确认')
                    var id = e.currentTarget.dataset.id
                    var index = e.currentTarget.dataset.index
                    util.httpRrquest(app.data.rooturl + 'order/cancelOrder', 'GET', { id: id })
                    .then(function (response) {
                        wx.showToast({
                            title: response.data.msg,
                            icon: 'none'
                        })
                        //修改列表状态
                        var lists = that.data.lists
                        lists[index]['status'] = 4;//设置此订单未取消 
                        that.setData({
                            lists: lists
                        })
                    }, function (error) {
                        console.log(error);
                    });
                } else if(res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    complete:function(e){//订单完成
        var that = this;
        wx.showModal({
            title: '提示',
            content: '确定已经提取此订单商品？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确认')
                    var id = e.currentTarget.dataset.id
                    var index = e.currentTarget.dataset.index
                    util.httpRrquest(app.data.rooturl + 'order/completeOrder', 'GET', { id: id })
                        .then(function (response) {
                            wx.showToast({
                                title: response.data.msg,
                                icon: 'none'
                            })
                            //修改列表状态
                            var lists = that.data.lists
                            lists[index]['status'] = 3;//设置此订单已完成
                            lists[index]['statusTitle'] = '已完成';//设置此订单已完成
                            that.setData({
                                lists: lists
                            })
                        }, function (error) {
                            console.log(error);
                        });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    buys:function(e){
        var that = this;
        var userid=app.globalData.userId;
        wx.showModal({
            title: '提示',
            content: '确定要支付此订单？',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确认')
                    var id = e.currentTarget.dataset.id
                    var onumber = e.currentTarget.dataset.number
                    util.httpRrquest(app.data.rooturl + 'order/weixinPay', 'GET', { onumber: onumber,userid:userid,isgopay:1 })
                    .then(function (response) {
                        var data = response.data.data;
                        var data = JSON.parse(data);
                        wx.requestPayment({
                            timeStamp: data.timeStamp,
                            nonceStr: data.nonceStr,
                            package: data.package,
                            signType: data.signType,
                            paySign: data.paySign,
                            success(res) {
                                //支付成功通知修改订单信息
                                util.httpRrquest(app.data.rooturl + 'order/success', 'GET', { onumber: onumber})
                                .then(function (response) {
                                    //支付成功跳转
                                    wx.redirectTo({
                                        url: '/pages/order/order?state=1',
                                    })
                                })
                            },
                            fail(res) {
                                wx.redirectTo({
                                    url: '/pages/order/order?state=1',
                                })
                            }
                        })
                         
                    }, function (error) {
                        console.log(error);
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            page: 0,
            lists: []
        })
        //获取订单列表
        this.getorders();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        //获取订单列表
        this.getorders();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    }
})